<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户" prop="deptId">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择商户">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
              ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否锁定" prop="isLock">
        <el-select v-model="queryParams.isLock" placeholder="请选择是否锁定" clearable>
          <el-option
            v-for="dict in bl_user_lock"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="false">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['bl:users:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bl:users:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bl:users:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button v-if="false"
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['bl:users:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blUsersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商" align="center" prop="sysDept.deptName" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户金额" align="center" prop="userMoney" />
      <el-table-column label="冻结金额" align="center" prop="frozenMoney" />
      <el-table-column label="用户系数" align="center" prop="ratio" />
      <el-table-column label="注册时间" align="center" prop="regTime" />
      <el-table-column label="最后登录时间" align="center" prop="lastLogin" />
      <el-table-column label="最后登录ip" align="center" prop="lastIp" v-if="false"/>
      <el-table-column label="是否验证手机" align="center" prop="mobileValidated" v-if="false">
        <template #default="scope">
          <dict-tag :options="sys_number_yes_no" :value="scope.row.mobileValidated"/>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="headPic" width="100" v-if="false">
        <template #default="scope">
          <image-preview :src="scope.row.headPic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="是否锁定" align="center" prop="isLock">
        <template #default="scope">
          <dict-tag :options="bl_user_lock" :value="scope.row.isLock"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bl:users:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bl:users:remove']" v-if="false">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="查看地址" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <router-link :to="'/bl/user-address/index/' + scope.row.userId" class="link-type">
            <span>地址详情</span>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="blUsersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商id" prop="suppliersId" v-if="false">
          <el-input v-model="form.suppliersId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName" v-if="false">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label=" 1 男 2 女 3未知" prop="sex"  v-if="false">
          <el-select v-model="form.sex" placeholder="请选择 1 男 2 女 3未知">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日" prop="birthday"  v-if="false">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户金额" prop="userMoney"  v-if="false">
          <el-input v-model="form.userMoney" placeholder="请输入用户金额" />
        </el-form-item>
        <el-form-item label="冻结金额" prop="frozenMoney"  v-if="false">
          <el-input v-model="form.frozenMoney" placeholder="请输入冻结金额" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile"  v-if="false">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="是否验证手机" prop="mobileValidated"  v-if="false">
          <el-select v-model="form.mobileValidated" placeholder="请选择是否验证手机">
            <el-option
              v-for="dict in sys_number_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="headPic"  v-if="false">
          <image-upload v-model="form.headPic"/>
        </el-form-item>
        <el-form-item label="第三方返回昵称" prop="nickname"  v-if="false">
          <el-input v-model="form.nickname" placeholder="请输入第三方返回昵称" />
        </el-form-item>
        <el-form-item label="用户系数" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入用户系数" />
        </el-form-item>
        <el-form-item label="是否锁定" prop="isLock">
          <el-select v-model="form.isLock" placeholder="请选择是否锁定">
            <el-option
              v-for="dict in bl_user_lock"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="blUsers">
import { listBlUsers, getBlUsers, delBlUsers, addBlUsers, updateBlUsers } from "@/api/bl/users/users";
import { listDeptAll } from "@/api/bl/dept";

const { proxy } = getCurrentInstance();
const { bl_user_lock, sys_number_yes_no, sys_user_sex } = proxy.useDict('bl_user_lock', 'sys_number_yes_no', 'sys_user_sex');

const blUsersList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: null,
    userName: null,
    sex: null,
    birthday: null,
    regTime: null,
    mobile: null,
    nickname: null,
    isLock: null,
  },
  rules: {
    ratio: [
      { required: true, message: "用户系数不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户信息列表 */
function getList() {
  if(queryParams.value.deptId === null || queryParams.value.deptId === undefined){
    return;
  }
  loading.value = true;
  listBlUsers(queryParams.value).then(response => {
    blUsersList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    deptId: null,
    suppliersId: null,
    companyId: null,
    sysUserId: null,
    userName: null,
    password: null,
    sex: null,
    birthday: null,
    userMoney: null,
    frozenMoney: null,
    regTime: null,
    lastLogin: null,
    lastIp: null,
    mobile: null,
    mobileValidated: null,
    openid: null,
    headPic: null,
    nickname: null,
    isLock: null,
    firstLeader: null,
    token: null,
    ratio: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("usersRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = row.userId || ids.value
  getBlUsers(_userId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["blUsersRef"].validate(valid => {
    if (valid) {
      if (form.value.userId != null) {
        updateBlUsers(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBlUsers(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除用户信息编号为"' + _userIds + '"的数据项？').then(function() {
    return delBlUsers(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bl/BlUsers/export', {
    ...queryParams.value
  }, `BlUsers_${new Date().getTime()}.xlsx`)
}

    /** 查询商户信息列表 */
function getDeptAllList() {
  listDeptAll(queryParams.value).then(response => {
    let deptListTmp = response.data;
    deptOptions.value = deptListTmp.filter(item => item.deptId !== 100);
  });
}

getDeptAllList();
//getList();
</script>
